package com.ruoyi.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.tws.api.domain.Order;
import lombok.RequiredArgsConstructor;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.web.page.Condition;
import com.ruoyi.tws.domain.OrderMonetaryChange;
import com.ruoyi.tws.service.IOrderMonetaryChangeService;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 运费变更申请Controller
 * 
 * @author wjx
 * @date 2023-05-10
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/monetaryChange" )
public class OrderMonetaryChangeController extends BaseController
{
    private final IOrderMonetaryChangeService iOrderMonetaryChangeService;

    /**
     * 查询运费变更申请列表
     */
    @PreAuthorize(hasPermi="tws:monetaryChange:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, OrderMonetaryChange orderMonetaryChange)
    {
        LambdaQueryWrapper<OrderMonetaryChange> lqw = new LambdaQueryWrapper<OrderMonetaryChange>();
        if (orderMonetaryChange.getOrderId() != null) {
            lqw.eq(OrderMonetaryChange::getOrderId, orderMonetaryChange.getOrderId());
        }
        if (StringUtils.isNotBlank(orderMonetaryChange.getShippingNoteNumber())) {
            lqw.eq(OrderMonetaryChange::getShippingNoteNumber, orderMonetaryChange.getShippingNoteNumber());
        }
        if (orderMonetaryChange.getDriverId() != null) {
            lqw.eq(OrderMonetaryChange::getDriverId, orderMonetaryChange.getDriverId());
        }
        if (StringUtils.isNotBlank(orderMonetaryChange.getDriverName())) {
            lqw.like(OrderMonetaryChange::getDriverName ,orderMonetaryChange.getDriverName());
        }
        if (StringUtils.isNotBlank(orderMonetaryChange.getDriverPhone())) {
            lqw.eq(OrderMonetaryChange::getDriverPhone, orderMonetaryChange.getDriverPhone());
        }
        if (orderMonetaryChange.getDesireMonetaryAmount() != null) {
            lqw.eq(OrderMonetaryChange::getDesireMonetaryAmount, orderMonetaryChange.getDesireMonetaryAmount());
        }
        if (orderMonetaryChange.getTotalMonetaryAmount() != null) {
            lqw.eq(OrderMonetaryChange::getTotalMonetaryAmount, orderMonetaryChange.getTotalMonetaryAmount());
        }
        if (orderMonetaryChange.getStatus() != null) {
            lqw.eq(OrderMonetaryChange::getStatus, orderMonetaryChange.getStatus());
        }
        lqw.orderByDesc(OrderMonetaryChange::getCreateTime);
        IPage<OrderMonetaryChange> list = iOrderMonetaryChangeService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 导出运费变更申请列表
     */
    @PreAuthorize(hasPermi = "tws:monetaryChange:export")
    @Log(title = "运费变更申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderMonetaryChange orderMonetaryChange) throws IOException
    {
        LambdaQueryWrapper<OrderMonetaryChange> lqw = new LambdaQueryWrapper<OrderMonetaryChange>(orderMonetaryChange);
        List<OrderMonetaryChange> list = iOrderMonetaryChangeService.list(lqw);
        ExcelUtil<OrderMonetaryChange> util = new ExcelUtil<OrderMonetaryChange>(OrderMonetaryChange. class);
        util.exportExcel(response, list, "monetaryChange");
    }

    /**
     * 获取运费变更申请详细信息
     */
    @PreAuthorize(hasPermi="tws:monetaryChange:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iOrderMonetaryChangeService.getById(id));
    }

    /**
     * 新增运费变更申请
     */
    @PreAuthorize(hasPermi="tws:monetaryChange:add")
    @Log(title = "运费变更申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderMonetaryChange orderMonetaryChange) {
        return toAjax(iOrderMonetaryChangeService.save(orderMonetaryChange) ? 1 : 0);
    }

    /**
     * 修改运费变更申请
     */
    @PreAuthorize(hasPermi="tws:monetaryChange:edit")
    @Log(title = "运费变更申请" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderMonetaryChange orderMonetaryChange) {
        return toAjax(iOrderMonetaryChangeService.updateById(orderMonetaryChange) ? 1 : 0);
    }

    /**
     * 删除运费变更申请
     */
    @PreAuthorize(hasPermi="tws:monetaryChange:remove")
    @Log(title = "运费变更申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iOrderMonetaryChangeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 变更运费申请
     */
    @Transactional
    @PostMapping("/updateMonetaryChange")
    public AjaxResult updateMonetaryChange(@RequestBody Order order){
        return toAjax(iOrderMonetaryChangeService.MonetaryChange(order) ? 1 : 0);
    }
    /**
     * 驳回
     * @param orderMonetaryChange
     * @return
     */
    @PostMapping("/rejectChange")
    @Transactional
    public AjaxResult rejectChange(@RequestBody OrderMonetaryChange orderMonetaryChange){
        return toAjax(iOrderMonetaryChangeService.rejectChange(orderMonetaryChange) ? 1 : 0);
    }

}
